package com.transaction.controller;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.transaction.config.R;
import com.transaction.domain.SysMarket;
import com.transaction.service.SysMarketService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/market")
public class SysMarketController {

    @Autowired
    private SysMarketService sysMarketService;

    /***
     * 股票行情查询&条件查询
     * @param page
     * @param sysMarket
     * @return
     */
    @PostMapping("/listMarket")
    public R listMarket(@RequestBody Page page,SysMarket sysMarket){
        //查询股票代码
        QueryWrapper<SysMarket> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ts_code",sysMarket.getTsCode());
        SysMarket one = sysMarketService.getOne(queryWrapper);
        Page page1 = sysMarketService.page(page, new LambdaQueryWrapper<SysMarket>(one));
        return R.successed(page1);
    }

    /***
     * 股票行情添加
     * @param sysMarket
     * @return
     */
    @PostMapping("/saveMarket")
    public R saveMarket(@RequestBody SysMarket sysMarket){
        long id = IdUtil.getSnowflake().nextId();
        //雪花算法自动生成股票代码
        sysMarket.setTsCode(Long.toString(id));
        sysMarket.setTradeDate(new Date());
        sysMarketService.save(sysMarket);
        return R.successed(null);
    }

    /***
     * 股票行情修改
     * @param sysMarket
     * @return
     */
    @PostMapping("/updateMarket")
    public R updateMarket(@RequestBody SysMarket sysMarket){
        sysMarketService.updateById(sysMarket);
        return R.successed(null);
    }

    /***
     * 股票行情删除
     * @param id
     * @return
     */
    @PostMapping("/removeMarketById")
    public R removeMarketById(Integer id){
        sysMarketService.removeById(id);
        return R.successed(null);
    }

    /***
     * 股票修改回显
     * @return
     */
    @PostMapping("/selectMarket")
    public R selectMarket(){
        List<SysMarket> list = sysMarketService.list();
        return R.successed(list);
    }

    /***
     * 股票导出
     * @param sysMarket
     * @return
     */
    @PostMapping("/export")
    public R export(@RequestBody SysMarket sysMarket, HttpServletResponse response){
        List<SysMarket> list = sysMarketService.listExport(sysMarket);
        sysMarketService.export(list,response);
        return R.successed(null);
    }

}
